import unittest
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCase01(unittest.TestCase):
    def setUp(self) -> None:
        global driver
        driver = webdriver.Chrome()
        driver.get("https://www.baidu.com")
    def tearDown(self) -> None:
        driver.quit()

    #百度的登录
    def test_01(self):
        sleep(2)
        driver.find_element(By.ID,'s-top-loginbtn').click()
        sleep(3)
        driver.find_element(By.XPATH,"//input[@id='TANGRAM__PSP_11__userName']").send_keys('15523698804')
        sleep(2)
        driver.find_element(By.XPATH,"//input[@id='TANGRAM__PSP_11__password']").send_keys('395713_FuAi')
        sleep(2)
        driver.find_element(By.XPATH,"//input[@name='isAgree']").click()
        sleep(3)
        driver.find_element(By.XPATH,"//input[@id='TANGRAM__PSP_11__submit']").click()


#百度多层级的定位
    def test_02(self):
        driver.find_element(By.XPATH,'//div[@id="s-hotsearch-wrapper"]/div/a[2]/span').click()
        sleep(2)
        driver.find_element(By.XPATH,' //ul[@id="hotsearch-content-wrapper"]/li[1]/a/span[2]').click()
        sleep(2)

#窗口切换
    def test_03(self):
        #获取当前窗口句柄
        h=driver.current_window_handle
        driver.find_element(By.XPATH,'//div[@id="s-top-left"]/a[4]').click()
        #隐式等待
        driver.implicitly_wait(10)
        sleep(2)
        #窗口切换为百度首页
        driver.switch_to.window(h)
        driver.implicitly_wait(5)
        sleep(2)

#失败失败
#定位frame/iframe里面的元素：1、通过框架id或者name切换  2、定位到框架的元素切换
    def test_04(self):
        #点击贴吧
        driver.find_element(By.XPATH, '//div[@id="s-top-left"]/a[4]').click()
        sleep(2)
        #通过框架id或者name切换
        # 假设 iframe 的 id 是 'iframeu6739266_0'
        iframe = driver.find_element(By.ID,'iframeu6739266_0')
        driver.switch_to.frame(iframe)
        sleep(2)
        # 在这里执行你需要在 iframe 内部进行的操作
        driver.find_element(By.XPATH,'//div[@id="iSlider-wrapper"]/url/li[1]/a/img').click()
        sleep(2)
        # 当操作完成后，返回默认的内容
        driver.switch_to.default_content()
        sleep(2)
